package com.rainbow.support.message.notify.config;

import com.rainbow.support.message.notify.dingtalk.DingTalkTemplate;
import com.rainbow.support.message.notify.email.MailTemplate;
import com.rainbow.support.message.notify.properties.MessageProperties;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
 * 消息通知配置
 * @author 傅为地
 */
@Configuration
@AutoConfigureBefore(MailSenderAutoConfiguration.class)
@EnableConfigurationProperties({MessageProperties.class})
@ConditionalOnProperty(prefix=MessageProperties.EXTRA_MESSAGE_NOTIFY_PREFIX,name = "enable", havingValue = "true",matchIfMissing = true)
public class MessageConfig {

    /**
     * 邮件配置
     * @param properties 邮件属性
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(JavaMailSender.class)
    @ConditionalOnExpression(MessageProperties.EXTRA_MESSAGE_NOTIFY_EMAIL_SPEL)
    public JavaMailSender mailSender(MessageProperties properties) {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(properties.getMail().getHost());
        if (properties.getMail().getPort() != null) {
            sender.setPort(properties.getMail().getPort());
        }
        sender.setUsername(properties.getMail().getUsername());
        sender.setPassword(properties.getMail().getPassword());
        sender.setProtocol(properties.getMail().getProtocol());
        if (properties.getMail().getDefaultEncoding() != null) {
            sender.setDefaultEncoding(properties.getMail().getDefaultEncoding().name());
        }
        if (!properties.getMail().getProperties().isEmpty()) {
            Properties javaMailproperties = new Properties();
            javaMailproperties.putAll(properties.getMail().getProperties());
            sender.setJavaMailProperties(javaMailproperties);
        }
        return sender;
    }

    /**
     * 邮件配置
     * @param javaMailSender 发送配置
     * @param mailProperties 邮件属性
     * @return
     */
    @Bean
    @Primary
    @ConditionalOnExpression(MessageProperties.EXTRA_MESSAGE_NOTIFY_EMAIL_SPEL)
    public MailTemplate mail(JavaMailSender javaMailSender, MessageProperties mailProperties) {
        return new MailTemplate(javaMailSender, mailProperties);
    }

    /**
     * 钉钉配置
     * @return
     */
    @Bean
    @ConditionalOnExpression(MessageProperties.EXTRA_MESSAGE_NOTIFY_DING_SPEL)
    public DingTalkTemplate ding(MessageProperties messageProperties) {
        return new DingTalkTemplate(messageProperties);
    }

}
